<?php

//收藏功能
function leizi_collect()
{
	global $table_prefix, $wpdb;
		
	$base_name = $_REQUEST['page'];
        
	$base_page = 'admin.php?page='.$base_name;
	$wp = get_bloginfo('wpurl');
	$table_name = $table_prefix."collect";
	$snshare_log_perpage=3;
	if(!empty($_POST['snshare_dellog']))
	{
		$snshare_sql="TRUNCATE $table_name;";
		mysql_query($snshare_sql);
	}
    $post_user = wp_get_current_user();
    $uid = $post_user->ID;
	$total_shares = $wpdb->get_var("SELECT COUNT(c_id) FROM $table_name where c_userid = ".$uid.";");
	if(empty($_REQUEST['snshare_page']) || $_REQUEST['snshare_page']<1)
		$snshare_page=1;
	else
		$snshare_page=$_REQUEST['snshare_page'];

	$offset = ($snshare_page-1) * $snshare_log_perpage;

				
	if(empty($_REQUEST['snshare_sortpage']) || $_REQUEST['snshare_sortpage']<1)
		$snshare_sortpage=1;
	else
		$snshare_sortpage=$_REQUEST['snshare_sortpage'];

	$total_pages = ceil($total_shares / $snshare_log_perpage);

	$snshare_sql="SELECT * FROM  $table_name where c_userid = ".$uid." ORDER BY c_time DESC LIMIT $offset,$snshare_log_perpage;";
	$snshare_logs = $wpdb->get_results($snshare_sql);

?>
<div class="wrap">

	<h2>收藏统计</h2><br />
        
	<table class="widefat">
		<thead>
			<tr>
			  <th>收藏标题</th>
			  <th>收藏网址</th>
			  <th>收藏图片</th>
			  <th>收藏时间</th>
			</tr>
		</thead>
		<tbody>
<?php
		if($snshare_logs) {
			$i = 0;
			foreach($snshare_logs as $snshare_log) {
				if($i%2 == 0) {
					$style = 'class="alternate"';
				}  else {
					$style = '';
				}

				$snshare_posttitle= $snshare_log->c_posttitle;
				$snshare_posturl= $snshare_log->c_posturl;
				$snshare_postimg= $snshare_log->c_postimg;
				$snshare_time= $snshare_log->c_time;

				echo "<tr $style>\n";
				echo "<td><a href=\"".$snshare_posturl."\" target=\"_blank\">".$snshare_posttitle."</a></td>\n";
				echo "<td><a href=\"".$snshare_posturl."\" target=\"_blank\">".$snshare_posturl."</a></td>\n";
				echo "<td><img src='".$snshare_postimg."' width='70' height='80' /></td>\n";
				echo "<td>".$snshare_time."</td>\n";
				echo "</tr>\n";
				$i++;
			}
			
		}
		else {
			echo '<tr><td colspan="5" align="center"><strong>'.__('没有相关记录', 'sinoshare').'</strong></td></tr>';
		}
?>
		</tbody>
	</table>
		<!-- <分页> -->
		<?php
			if($total_pages > 1) {
		?>
		<br />
		<table class="widefat">
			<tr>
				<td align="left" width="30%">
					<?php
						if($snshare_page > 1 && ((($snshare_page*$snshare_log_perpage)-($snshare_log_perpage-1)) <= $total_shares)) {
							echo '<strong>&laquo;</strong> <a href="'.$base_page.'&amp;snshare_page='.($snshare_page-1).'&amp;snshare_sortpage='.$snshare_sortpage.'" title="&laquo; '.__('前一页', 'sinoshare').'">'.__('前一页', 'sinoshare').'</a>';
						} else {
							echo '&nbsp;';
						}
					?>
				</td>
                <td colspan="2" align="center" width="30%">
					<?php printf(__('总页数 (%s): ', 'sinoshare'), $total_pages); ?>
					<?php
						if ($snshare_page >= 4) {
							echo '<strong><a href="'.$base_page.'&amp;snshare_page=1'.'&amp;snshare_sortpage='.$snshare_sortpage.'" title="'.__('第一页', 'sinoshare').'">&laquo; '.__('第一页', 'sinoshare').'</a></strong> ... ';
						}
						if($snshare_page > 1) {
							echo ' <strong><a href="'.$base_page.'&amp;snshare_page='.($snshare_page-1).'&amp;snshare_sortpage='.$snshare_sortpage.'" title="&laquo; '.__('第', 'sinoshare').($snshare_page-1).__('页', 'sinoshare').'">&laquo;</a></strong> ';
						}
						for($i = $snshare_page - 2 ; $i  <= $snshare_page +2; $i++) {
							if ($i >= 1 && $i <= $total_pages) {
								if($i == $snshare_page) {
									echo '<strong>['.$i.']</strong> ';
								} else {
									echo '<a href="'.$base_page.'&amp;snshare_page='.($i).'&amp;snshare_sortpage='.$snshare_sortpage.'" title="'.__('第', 'sinoshare').$i.__('页', 'sinoshare').'">'.$i.'</a> ';
								}
							}
						}
						if($snshare_page < $total_pages) {
							echo ' <strong><a href="'.$base_page.'&amp;snshare_page='.($snshare_page+1).'&amp;snshare_sortpage='.$snshare_sortpage.'" title="'.__('第', 'sinoshare').($snshare_page+1).__('页', 'sinoshare').' &raquo;">&raquo;</a></strong> ';
						}
						if (($snshare_page+2) < $total_pages) {
							echo ' ... <strong><a href="'.$base_page.'&amp;snshare_page='.($total_pages).'&amp;snshare_sortpage='.$snshare_sortpage.'" title="'.__('最后一页', 'sinoshare').'">'.__('最后一页', 'sinoshare').' &raquo;</a></strong>';
						}
					?>
				</td>
				<td align="right" width="30%">
					<?php
						if($snshare_page >= 1 && ((($snshare_page*$snshare_log_perpage)+1) <=  $total_shares)) {
							echo '<a href="'.$base_page.'&amp;snshare_page='.($snshare_page+1).'&amp;snshare_sortpage='.$snshare_sortpage.'" title="'.__('下一页', 'sinoshare').' &raquo;">'.__('下一页', 'sinoshare').'</a> <strong>&raquo;</strong>';
						} else {
							echo '&nbsp;';
						}
					?>
				</td>
			</tr>
		</table>
		<?php
			}
		?>
		<!-- </分页> --><br /><br />
		<div align="left">
		  <form name="snshare_buttonform" method="post" action="<?php echo $base_page?>">
		   <input type="submit" name="snshare_dellog" value="<?php print('清空全部记录'); ?>" class="button" onclick="javascript:snshare_buttonform.action='<?php echo $base_page?>';snshare_buttonform.target='';return confirm('真的要删除全部记录吗？');"/>
		   </form>
		</div>


</div>
<?php 
}

function leizi_add_collect()
{
        global $table_prefix, $wpdb, $post;
        
        if(has_post_thumbnail()) 
        {
            //$thumbnail =  get_the_post_thumbnail($post->ID,'thumbnail'); 
            $post_thumbnail = get_post_thumbnail_id( $post->ID );
            $image_thumbnail = wp_get_attachment_image_src( $post_thumbnail,'larger' );
        }
        else
        {
            $thumbnail = '无图片';
        }
        
        $ip = getenv("REMOTE_ADDR");
      
    ?>
        <script>
        function addcollect<?php echo $post->ID;  ?>()
        {
            var posturl = $('.posturl<?php echo $post->ID;  ?>').val();
            var posttitle = $('.posttitle<?php echo $post->ID;  ?>').val();
            var postimg = $('.postimg<?php echo $post->ID;  ?>').val();
            var userid = $('.userid').val();
			var metavalue = $('.metavalue').val();
            var postid = $('.postid<?php echo $post->ID;  ?>').val();
    
            $.ajax({
               type: "POST",
               url: "<?php echo get_bloginfo('template_url'); ?>/inc/check-info.php",
               data: "posturl="+posturl+"&posttitle="+posttitle+"&postimg="+postimg+"&userid="+userid+"&metavalue="+metavalue+"&postid="+postid,
               async: false,
               error: function(msg){ alert('msg');},
               success: function(text){
                 response = text;
               }
            }); 
            if((/^(\+|-)?\d+$/.test( response ))|| response<0)
            {  
               alert('该视频收藏成功！');
			   $('#229_likenum').html('<span>'+response+'</span>');   
            }
            else
            {  
                alert(response);    
            }  
    
        }
        
        </script>
        <?php 
        $current_user = wp_get_current_user();
		$snshare_sql="SELECT meta_value FROM wp_users as a1 join wp_usermeta as a2 on a1.ID = a2.user_id where a1.ID = ".$current_user->ID." and meta_key='primary_blog'";
		$metavalue = $wpdb->get_results($snshare_sql);
		$collect = get_post_meta($post->ID, '_postcollect', true);
		if(empty($collect)){ $collect = 0;}else{ $collect = $collect; }
		
        if(is_single())
        {
            $content .= '<a class="info_num blue"><span id="229_likenum">'.$collect.'</span></a>';
        }
		else
		{
			$content .= '<a href="javascript:void(0);" onclick="addcollect'.$post->ID.'();" >收藏</a>';
		}   
        ?>
        <input type="hidden" value="<?php echo get_permalink($post->ID);  ?>" name="posturl" class="posturl<?php echo $post->ID;  ?>" />
        <input type="hidden" value="<?php echo get_the_title($post->ID);  ?>" name="posttitle" class="posttitle<?php echo $post->ID;  ?>" />
        <input type="hidden" value="<?php echo $image_thumbnail[0];  ?>" name="postimg" class="postimg<?php echo $post->ID;  ?>" />
        <input type="hidden" value="<?php echo $current_user->ID;  ?>" name="userid" class="userid" />
        <input type="hidden" value="<?php echo $post->ID;  ?>" name="postid" class="postid<?php echo $post->ID;  ?>" />
<?php
        //return $content;.
         echo apply_filters( 'leizi_add_collect', $content );
}
//add_action('the_content', 'leizi_add_collect');
 ?>